package ch.elektroplan.cmueller.logikModulGenerator.presentation.subs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ch.elektroplan.cmueller.logikModulGenerator.business.elements.Output;

public class OutputPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Output output;

	private JLabel lb_name;
	private JTextField tf_name;

	private JLabel lb_comment;
	private JTextField tf_comment;

	private JLabel lb_dataformat;
	private JComboBox cb_dataformat;
	private String[] dataformat = { "nummerisch", "text" };

	private JLabel lb_round;
	private JComboBox cb_round;
	private String[] round_change = { "ja", "nein" };

	private JLabel lb_sendonChange;
	private JComboBox cb_sendonChange;

	private JLabel lb_initialisationValue;
	private JTextField tf_initialisationValue;

	public OutputPanel(Output output) {

		this.output = output;
		this.setLayout(new GridLayout(2, 6));
		this.setPreferredSize(new Dimension(800, 50));

		this.lb_name = new JLabel("name");

		this.tf_name = new JTextField();
		this.tf_name.addFocusListener(new LostFocusListener(this.tf_name));

		this.lb_initialisationValue = new JLabel("initval");

		this.tf_initialisationValue = new JTextField();
		this.tf_initialisationValue.addFocusListener(new LostFocusListener(
				this.tf_initialisationValue));

		this.lb_comment = new JLabel("Kommentar");

		this.tf_comment = new JTextField();
		this.tf_comment
				.addFocusListener(new LostFocusListener(this.tf_comment));

		this.lb_round = new JLabel("runden");

		this.cb_round = new JComboBox(round_change);
		this.cb_round.addFocusListener(new LostFocusListener(cb_round));

		this.lb_sendonChange = new JLabel("sendcha");

		this.cb_sendonChange = new JComboBox(round_change);
		this.cb_sendonChange.addFocusListener(new LostFocusListener(
				cb_sendonChange));

		this.lb_dataformat = new JLabel("dataform");

		this.cb_dataformat = new JComboBox(dataformat);
		this.cb_dataformat
				.addFocusListener(new LostFocusListener(cb_dataformat));

		
		
		this.add(lb_name);
		this.add(lb_dataformat);
		this.add(lb_initialisationValue);
		this.add(lb_round);
		this.add(lb_sendonChange);
		this.add(lb_comment);
		
		this.add(tf_name);
		this.add(cb_dataformat);
		this.add(tf_initialisationValue);
		this.add(cb_round);
		this.add(cb_sendonChange);
		this.add(tf_comment);
	}

	private class LostFocusListener implements FocusListener {

		private JComponent jComponent;

		public LostFocusListener(JComponent jComponent) {
			this.jComponent = jComponent;
		}

		@Override
		public void focusGained(FocusEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void focusLost(FocusEvent e) {

			if (jComponent.equals(tf_name)) {
				JTextField tf = (JTextField) jComponent;
				output.setName(tf.getText());
			}
			if (jComponent.equals(tf_initialisationValue)) {
				JTextField tf = (JTextField) jComponent;
				output.setInitialisationValue(tf.getText());
			}
			if (jComponent.equals(tf_comment)) {
				JTextField tf = (JTextField) jComponent;
				output.setComment(tf.getText());
			}
			if (jComponent.equals(cb_dataformat)) {
				JComboBox cb = (JComboBox) jComponent;
				output.setDataformat(cb.getSelectedIndex());
			}
			if (jComponent.equals(cb_round)) {
				JComboBox cb = (JComboBox) jComponent;
				output.setDataformat(cb.getSelectedIndex());
			}
			if (jComponent.equals(cb_sendonChange)) {
				JComboBox cb = (JComboBox) jComponent;
				output.setDataformat(cb.getSelectedIndex());
			}
		}

	}
}
